/**
  ******************************************************************************
  * @file   bsp_sht20.c
  * @brief  温湿度传感器SHT20驱动
  *   
  ******************************************************************************
  */
#include "bsp_sht20.h"
#include "i2c.h"
#include "stdio.h"
#include "usart.h"
#include "string.h"
#include "wifi.h"
#include "bsp_ili9341_4line.h"
#include "spi.h"
#include "bsp_ap3216c.h"
//
volatile SHT20_TemRH_Val gTemRH_Val;	//存储温湿度值
//
/*
**********************************************************************
* @fun     :BSP_SHT20_Read 
* @brief   :根据指令读取sht20内部的寄存器
* @param   :
* @return  :sht20_reg_val 
**********************************************************************
*/
uint16_t BSP_SHT20_Read(uint8_t sht20_cmd)
{
		uint16_t sht20_reg_val = 0;
		uint8_t sht20_reg_buff[2]={0x00,0x00};
		HAL_I2C_Master_Transmit(&hi2c1,SHT20_ADDR_WRITE,&sht20_cmd,1,100);    //发送控制指令
		HAL_Delay(10);
		HAL_I2C_Master_Receive(&hi2c1,SHT20_ADDR_READ,sht20_reg_buff,2,100);      //读取数据，两个字节
		HAL_Delay(10);
		sht20_reg_val=(sht20_reg_buff[0]<<8)|sht20_reg_buff[1];										//合并数据
	
		return (sht20_reg_val);
}
/*
**********************************************************************
* @fun     :BSP_SHT20_GetData 
* @brief   :获取sht20的温湿度数据
* @param   :
* @return  :none
**********************************************************************
*/
void BSP_SHT20_GetData(void)
{

	extern uint8_t fire;
	extern int beep;
	extern int heat;
	extern int light;
	extern int fan;
	extern char msg[256];
	extern volatile int flag;
	extern volatile int Tempup;
  extern volatile int Tempdown;
	extern volatile int Humiup;
	extern volatile int Humidown;
	
	extern uint8_t temBuf[32];
	extern uint8_t humBuf[32];
	extern uint8_t illumBuf[32];
	extern uint8_t fanBuf[32];
	extern uint8_t beepBuf[32];
	extern uint8_t heating[32];
	extern uint8_t lightBuf[32];
	extern uint8_t fireBuf[32];
	
	uint16_t  pTem=0,pHum=0;
	//读取数据
	pTem=BSP_SHT20_Read(SHT20_HOLD_M_READ_T);
	pHum=BSP_SHT20_Read(SHT20_HOLD_M_READ_RH);
	pHum &=0xFFFC;
	//数据转换
	gTemRH_Val.Tem	= -46.85f+175.72f*((float)pTem/65536);
	gTemRH_Val.Hum  = -6+125*((float)pHum/65536);
	

	fire = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_3);
	
	if(flag == 0)//自动模式
	{
	if(gTemRH_Val.Tem < Tempup)
	{
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);//开加热灯
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,0);//关风扇
	}
	else if(gTemRH_Val.Tem > Tempup)
	{
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,1);//开风扇
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);//关加热灯
	}
	
	if(gTemRH_Val.Hum > Humiup)
	{
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);//开加热灯
		HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,1);//开风扇
	}
	
	if(fire == 1)
	{
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15,1);
	}
	else if(fire == 0)
	{
		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_15,0);
	}
}
	
	beep = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_15);
	heat = HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_4);
	light = HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13);
	fan = HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_6);
			
		sprintf(msg,"temp:%.4f\nhumi:%.4f\n",gTemRH_Val.Tem,gTemRH_Val.Hum);
	  USART_printf(&huart1,msg);
	  WIFI_SendData(msg);

    printf("Temup=%d\n",Tempup);
    printf("Humup=%d\n",Humiup);

			
			if(beep==1)
		{
			sprintf(msg,"beep:on\n");
	  USART_printf(&huart1,msg);	
	  WIFI_SendData(msg);
		}else{
			sprintf(msg,"beep:off\n");
	  USART_printf(&huart1,msg);	
	  WIFI_SendData(msg);
		}
		
		if(heat==1)
		{
			sprintf(msg,"heat:on\n");
	  USART_printf(&huart1,msg);	
	  WIFI_SendData(msg);
		}else{
			sprintf(msg,"heat:off\n");
	  USART_printf(&huart1,msg);	
	  WIFI_SendData(msg);
		}
			
			if(light==1)
		{
			sprintf(msg,"light:on\n");
	  USART_printf(&huart1,msg);	
	  WIFI_SendData(msg);
		}else{
			sprintf(msg,"light:off\n");
	  USART_printf(&huart1,msg);	
	  WIFI_SendData(msg);
		}

		if(fan==1)
		{
			sprintf(msg,"fan:on\n");
	  USART_printf(&huart1,msg);	
	  WIFI_SendData(msg);
		}else{
			sprintf(msg,"fan:off\n");
	  USART_printf(&huart1,msg);	
	  WIFI_SendData(msg);
		}
		
		if(fire==1)
		{
			sprintf(msg,"fire:on\n");
	  USART_printf(&huart1,msg);	
	  WIFI_SendData(msg);
		}else{
			sprintf(msg,"fire:off\n");
	  USART_printf(&huart1,msg);	
	  WIFI_SendData(msg);
		}
		

		
		sprintf(temBuf,"%.2f\n",gTemRH_Val.Tem);
		sprintf(humBuf,"%.2f\n",gTemRH_Val.Hum);
		sprintf(fanBuf,"%d\n",fan);
		sprintf(beepBuf,"%d\n",beep);
		sprintf(heating,"%d\n",heat);
		sprintf(lightBuf,"%d\n",light);
		sprintf(fireBuf,"%d\n",fire);
		Gui_DrawFont_GBK16(55,65,BLACK,WHITE,temBuf);
		Gui_DrawFont_GBK16(55,107,BLACK,WHITE,humBuf);
		Gui_DrawFont_GBK16(155,65,BLACK,WHITE,fanBuf);
		Gui_DrawFont_GBK16(165,107,BLACK,WHITE,heating);
		Gui_DrawFont_GBK16(65,147,BLACK,WHITE,lightBuf);
		Gui_DrawFont_GBK16(163,148,BLACK,WHITE,beepBuf);
		Gui_DrawFont_GBK16(75,188,BLACK,WHITE,fireBuf);

		
		
		
		
		
		//火焰感应值 Todo
	//sprintf(msg,"{\"data\":[{\"name\":\"%s\",\"val\":\"%d\"},{\"name\":\"%s\",\"val\":\"%d\"},{\"name\":\"%s\",\"val\":\"%d\"},{\"name\":\"%s\",\"val\":\"%d\"},{\"name\":\"%s\",\"val\":\"%d\"},{\"name\":\"%s\",\"val\":\"%d\"},]}","illum",22,"fire",22,"beep",beep,"heat",heat,"light",light,"fan",fan);

}

